package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.product.pojo.Brand;
import com.spzx.product.pojo.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-03-12
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {
    @Autowired(required = false)
    private CategoryBrandMapper categoryBrandMapper;

    @Override
    public List<CategoryBrand> selectCategoryBrandList(String name) {

        return categoryBrandMapper.selectCategoryBrandList(name);
    }

    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {
        // 验证分类品牌是否已存在
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId());
        Long count = categoryBrandMapper.selectCount(queryWrapper);

        if (count > 0) {
            throw new RuntimeException("该分类下该品牌已存在");
        }

        return categoryBrandMapper.insert(categoryBrand);
    }

    @Override
    public List<Brand> selectBrandListByCategoryId(Long categoryId) {
        return baseMapper.selectBrandListByCategoryId(categoryId);
    }
}
